package icu.xblogs.report.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtil {
    public static Calendar getNextMonth(String month){
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM").parse(month));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH,1);
        return cal;
    }
    public static int getMonthDay(String month){
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM").parse(month));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取某月最大天数
        int monthDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return monthDay;
    }
    public static int getCountDay(String month){
        Calendar today = Calendar.getInstance();
        Calendar nextMonth = DateUtil.getNextMonth(month);
        int countDay = DateUtil.getMonthDay(month);
        if(today.compareTo(nextMonth) == -1){
            countDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        }
        return countDay;
    }
}
